// pages/pic/pic.js
var app = getApp()
Page({
  data:{
    https:wx.getStorageSync('https'),
    banner:wx.getStorageSync('banner'),
    pics:'',    //存放图片集合
    WW:0,       //可用屏幕宽度
    left_h:0,
    right_h:0,
    arr_left:[],
    arr_right:[],
    pnow:1,
    Flower_Stat:'', //当前相册是否被赞状态
    Collect_Stat:'',//当前相册是否被收藏状态
    album_flower:0,   //当前相册的赞数
    album_collect:0   //当前相册的收藏数
  },

  //获取当前相册的赞和收藏状态
  Get_Flower_Collect(t){
    wx.request({
      url:t.data.https+'get_user_flower_collect',
      data:{
        openid:t.data.openid
      },
      success:function(res){
        var Flower_Stat = 0;
        var Collect_Stat = 0;
        var album_flower = t.data.album_flower;
        var album_collect = t.data.album_collect;
        if(res.data.Flower_Aids != null){
          for(var i=0;i<res.data.Flower_Aids.length;i++){
            if(res.data.Flower_Aids[i] == t.data.album_id){
               Flower_Stat = 1
            }
          }
          album_flower = res.data.Flower_Aids.length
        }

        if(res.data.Collect_Aids != null){
          for(var i=0;i<res.data.Collect_Aids.length;i++){
            if(res.data.Collect_Aids[i] == t.data.album_id){
                Collect_Stat = 1
            }
          }
          album_collect = res.data.Collect_Aids.length
        }
        wx.request({
          url:t.data.https+'get_flower_collect',
          data:{
            aid:t.data.album_id,
            field:'album_flower,album_collect'
          },
          success:function(res2){
            t.setData({
              Flower_Stat:Flower_Stat,
              Collect_Stat:Collect_Stat,
              album_collect:res2.data.album_collect,
              album_flower:res2.data.album_flower
            })
          }
        })
      }
    });
  },

  onLoad:function(o){
    var t = this
    this.setData({
      album_id:o.album_id,
      album_name:o.album_name,
      album_banner:o.album_banner,
      album_flower:o.album_flower,
      album_collect:o.album_collect,
      openid:o.openid
    })





    //获取屏幕宽度
    wx.getSystemInfo({
      success: function(res) {
        var WW = res.windowWidth
        t.setData({
          WW:WW
        })
      }
    })
    t.Get_Flower_Collect(t)

    t.GetPics(t.data.album_id)
  },

  getinfo(){
    var t = this
    wx.login({

      success:function(res1){
        var code = res1.code;
        wx.request({
          url:'https://photo.tobeyoung.net/NewAlbums/index.php/Home/Interface/Get_Openid',
          data:{
            code:code
          },
          success:function(res){
              t.setData({
                openid:res.data.openid,
              })

          }
        })
      }
    })
  },



  //获取相册图片
  GetPics(aid){
    var t = this
    wx.request({
      url:t.data.https+'get_album_pics',
      data:{
        'aid':aid,
        pnow:this.data.pnow
      },
      success:function(res){

        var pics = res.data.list
        if(!pics){
          return
        }
        if(pics.length > 0){
          t.setData({
            pics:pics,
            pnow:t.data.pnow + 1,
          })
        }

      }
    })
  },



  loadImg(e){
    var pid = e.currentTarget.dataset.pid
    var pics = this.data.pics
    var height = e.detail.height
    var width = e.detail.width

    var pic_w = this.data.WW * 0.48;
    var scale = pic_w / width;
    var pic_h = parseInt((height * scale).toFixed(0));

    var arr_left=this.data.arr_left
    var arr_right=this.data.arr_right
    var left_h=this.data.left_h
    var right_h=this.data.right_h
    for(var i=0;i<pics.length;i++){
      if(pics[i].imgs_id == pid){
        pics[i].pic_w = pic_w
        pics[i].pic_h = pic_h
        if(this.data.left_h <= this.data.right_h){
          arr_left.push(pics[i]);
          left_h = left_h + pic_h
          this.setData({
            arr_left:arr_left,
            left_h:left_h
          })          
        }else{
          arr_right.push(pics[i]);
          right_h = right_h + pic_h
          this.setData({
            arr_right:arr_right,
            right_h:right_h
          })  
        }
      }
    }
  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.GetPics(this.data.album_id)
  },



  //图片满屏预览效果
  Load_Img(e){
    var index = e.target.dataset.index
    var block = e.target.dataset.block
    var t = this
    var imgs = [];
    wx.request({
      url:t.data.https+'get_album_pics_all',
      data:{
        'aid':t.data.album_id,
      },
      success:function(res){
        for(var i=0;i<res.data.length;i++){
          imgs.push(t.data.banner+res.data[i].imgs_src)
        }
        if(block=='left'){
          var current = t.data.banner+ t.data.arr_left[index].imgs_src
        }else{
          var current = t.data.banner+ t.data.arr_right[index].imgs_src
        }
        wx.previewImage({
          current: current, // 当前显示图片的http链接
          urls: imgs // 需要预览的图片http链接列表
        })
      }
    })
  },



  //点赞状态切换
  Flower(e){
    var t = this
    var aid = e.currentTarget.dataset.aid
    var openid = this.data.openid
    wx.request({
      url:t.data.https+'set_flower',
      data:{
        aid:aid,
        openid:openid
      },
      success:function(res){
        t.Get_Flower_Collect(t)
      }
    });
  },


  //点赞状态切换
  Collect(e){
    var t = this
    var aid = e.currentTarget.dataset.aid
    var openid = this.data.openid
    wx.request({
      url:t.data.https+'set_collect',
      data:{
        aid:aid,
        openid:openid
      },
      success:function(res){
        t.Get_Flower_Collect(t)
      }
    });
  }




})